package com.sky.service.admin.impl;

import com.sky.mapper.admin.ReportMapper;
import com.sky.service.admin.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderDayAmountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.List;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2024/10/23 9:01
 * @Version 1.0
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public BusinessDataVO businessData() {
        LocalDate now = LocalDate.now();
        List<OrderDayAmountVo> orderDayAmountVos = reportMapper.orderStatisitcs(now, now.plusDays(1));
        if(CollectionUtils.isEmpty(orderDayAmountVos)){
            return new BusinessDataVO();
        }
        OrderDayAmountVo orderDayAmountVo = orderDayAmountVos.get(0);

        int completeNum = orderDayAmountVo.getCompleteNum();
        int totalNum = orderDayAmountVo.getTotalNum();
        double price = 0;
        if(completeNum != 0) {
            price = orderDayAmountVo.getAmount().doubleValue() / completeNum;
        }
        //封装新增用户数
        Integer newUser = reportMapper.getNewUser(now);

        BusinessDataVO businessDataVO = BusinessDataVO.builder().validOrderCount(orderDayAmountVo.getCompleteNum())
                .turnover(orderDayAmountVo.getAmount().doubleValue())
                .orderCompletionRate((double) completeNum / totalNum)
                .unitPrice(price)
                .newUsers(newUser).build();


        return businessDataVO;
    }

    @Override
    public DishOverViewVO dishOverView() {
        return reportMapper.dishStatusCount();
    }

    @Override
    public DishOverViewVO setmealOverView() {
        return reportMapper.setmealStatusCount();
    }
}